//
//  DataExtension.swift
//  JiaoZiTaxi
//
//  Created by Hu Gang on 2022/10/6.
//

import Foundation

private let yearFormatter: DateFormatter = {
  let formatter = DateFormatter()
  formatter.dateFormat = "yyyy 年 M 月 d 日"
  return formatter
}()

private let timeFormatter: DateFormatter = {
  let formatter = DateFormatter()
  formatter.dateFormat = "HH:mm"
  return formatter
}()

extension Date {
    var secondsSince1970:Int64 {
        Int64(self.timeIntervalSince1970.rounded())
    }

  var formatString: String {
    let date = Date()
    let components = Calendar.current.dateComponents([.year, .month, .day], from: self, to: date)

    if components.year! >= 1 || components.month! >= 1 || components.day! > 1 {
      return "\(yearFormatter.string(from: self)) \(timeFormatter.string(from: self))"
    } else if components.day == 1 {
      return "昨天 \(timeFormatter.string(from: self))"
    } else {
      return timeFormatter.string(from: self)
    }
  }

    var toString:String {
        let formatter = DateFormatter()
        formatter.dateFormat = "yyMMddHHmmss"
        return formatter.string(from: self)
    }

    var todayString:String {
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyyMMdd"
        return formatter.string(from: self)
    }

    var toStandardString:String {
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
        let v = formatter.string(from: self)
        return v
    }
}
